package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.awt.GridLayout;
import java.util.Collection;
import java.util.SortedSet;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import se.webbzon.boltzmann.editor.component.imagelist.ImageSetEntry;
import se.webbzon.boltzmann.editor.component.imagelist.ImageSetList;
import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.object.background.Background;
import se.webbzon.oschi01.worldobject.WorldObject;

public class BackgroundPropertyFrameFactory implements PropertyFrameFactory {

	// A sorted set of all the available images
	private final SortedSet<ImageSetEntry> images;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new background property frame factory with a given
	 * set of images. **/
	public BackgroundPropertyFrameFactory(SortedSet<ImageSetEntry> images) {
		this.images = images;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return Background.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		return new PropertyFrameImpl(model, (Background) instance, images);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends WorldObjectPropertyFrame {

		// The text fields of this property frame
		private final JTextField 	widthInput, heightInput, 
									panningInput, tiltInput;
		
		// The color chooser of this property frame
		private final JColorChooser	colorInput;
		
		// The image set panel of this property frame
		private final ImageSetList imagePanel;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(	EditorModel model, 
									Background instance,
									SortedSet<ImageSetEntry> images) {
			super(model, instance);
			
			// Create the background settings panel
			JPanel backgroundPanel = new JPanel();
			backgroundPanel.setLayout(new BoxLayout(backgroundPanel,BoxLayout.Y_AXIS));
			
			// Create settings panel
			JPanel settingsPanel = new JPanel();
			settingsPanel.setLayout(new GridLayout(4,2));
			
			settingsPanel.add(new JLabel("Width:"));
			settingsPanel.add(new JLabel("Height:"));
			widthInput = new JTextField("" + instance.getWidth());
			heightInput = new JTextField("" + instance.getHeight());
			settingsPanel.add(widthInput);
			settingsPanel.add(heightInput);
			
			settingsPanel.add(new JLabel("Maximal Panning:"));
			settingsPanel.add(new JLabel("Maximal Tilt:"));
			panningInput = new JTextField("" + instance.getPanningMax());
			tiltInput = new JTextField("" + instance.getTiltMax());
			settingsPanel.add(panningInput);
			settingsPanel.add(tiltInput);
			backgroundPanel.add(settingsPanel);
			
			// Create spacing
			backgroundPanel.add(Box.createVerticalStrut(32));
			
			// Create color input
			backgroundPanel.add(new JLabel("Background Color:"));
			colorInput = new JColorChooser(instance.getColor());
			backgroundPanel.add(colorInput);
			
			// Create the background selection panel
			imagePanel = 
					new ImageSetList(images,instance.getBackgroundName(),0,0);
			
			addPanel("Background Settings",backgroundPanel);
			addPanel(imagePanel.getName(),imagePanel);
			
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/

		@Override public boolean apply(Collection<String> errorMessages) {
			try {
				final double width, height, panningMax, tiltMax;
				width = Double.parseDouble(widthInput.getText());
				height = Double.parseDouble(heightInput.getText());
				panningMax = Double.parseDouble(panningInput.getText());
				tiltMax = Double.parseDouble(tiltInput.getText());
				
				Background background = (Background) getInstance();
				background.setWidth(width);
				background.setHeight(height);
				background.setPanningMax(panningMax);
				background.setTiltMax(tiltMax);
				background.setColor(colorInput.getColor());
				background.setBackground(imagePanel.getImageSetName());
				return true;
			} catch (NumberFormatException e) {
				errorMessages.add(e.getMessage());
				return false;
			}
		}
		
	}

}
